app.controller('checkCtrl', ['$scope','$stateParams','checkService','toaster',function($scope,$stateParams,checkService,toaster) {
    $scope.auth=$stateParams.auth;
    $scope.checkStatus=0;

    var t;
    var hideBox=function(){
        if(t) clearTimeout(t);
        t=setTimeout(function(){
            $scope.$apply(function(){
                $scope.checkStatus=0;
            })
        },2000);
    }

    //查询核销订单信息
    $scope.getcheckOrders=function(){
        if($scope.auth==''){
            return;
        }

        $scope.app.loading=true;

        checkService.getcheckOrders($scope).then(function (result) {
            switch (result.status){
                case 200:
                    $scope.checkOrders=result.data;
                    $scope.app.loading=false;

                    if(!$scope.updateLoading){
                        if($scope.checkOrders==null){
                            $scope.checkStatus=1;
                            hideBox();
                            return;
                        }
                        if($scope.checkOrders.status==5){
                            $scope.checkStatus=2;
                            hideBox();
                            return;
                        }
                    }

                    $scope.updateLoading=false;

                    break;
                default :
                    $scope.app.loading=false;
                    break;
            }
        })
    }

    $scope.getcheckOrders();


    $scope.updateLoading=false;
    $scope.authCheckOrders=function(){


        if(angular.isUndefined($scope.checkOrders) || $scope.checkOrders==null){
            $scope.getcheckOrders();
            return;
        }

        if(  $scope.checkOrders.status==5){
            $scope.getcheckOrders();
            return;
        }

        if($scope.updateLoading){
            return;
        }

        $scope.updateLoading=true;

        checkService.authCheckOrders($scope).then(function (result) {
            switch (result.status){
                case 200:
                    $scope.getcheckOrders();
                    $scope.checkStatus=3;
                    hideBox();
                    break;
                default :
                    $scope.app.updateLoading=false;
                    break;
            }
        })

    }



}]);